package com.ybkj.daijia.api.member.v3;

import com.ybkj.daijia.api.member.ErrorConstant;
import com.ybkj.daijia.api.member.SecurityUtils;
import com.ybkj.daijia.api.member.transport.ResultTransport;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.service.ActivityService;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.MemberAccountService;
import com.ybkj.daijia.server.service.MemberService;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("ActivityV3Controller")
@RequestMapping(value = "member/api/rest/v3")
public class ActivityController {

    private static final Logger logger = LoggerFactory.getLogger(ActivityController.class);

    @Autowired
    private ActivityService activityService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberAccountService memberAccountService;

    @Autowired
    private CompanyService companyService;


    /**
     * 查询活动
     *
     * @param phone
     * @param page
     * @param limit
     * @param appKey
     * @param token
     * @param timestamp
     * @return
     */
    @RequestMapping(value = "queryActivitys", method = RequestMethod.POST)
    public @ResponseBody
    ResultTransport queryActivitys(String phone, String acKey, Integer page, Integer limit,
        String appKey, String token, String timestamp) {

        logger
            .debug("recv phone:{},page:{},limit:{},appKey:{},token:{},timestamp:{}", phone, appKey,
                token, timestamp);

        if (StringUtils.isBlank(phone) || SecurityUtils.isBlank(appKey, token, timestamp)) {
            return ResultTransport.getErrorBy(ErrorConstant.PARAM_ERROR);
        }

        List<String> params = new LinkedList<String>();
        params.add(String.valueOf(phone));
        params.add(String.valueOf(page));
        params.add(String.valueOf(limit));
        params.add(timestamp);
        params.add(appKey);

        if (!SecurityUtils.checkToken(params, token)) {
            return ResultTransport.getErrorBy(ErrorConstant.TOKEN_ERROR);
        }

        Passenger passenger = memberService.findOnePassengerByPhone(phone);

        if (null == passenger) {
            return ResultTransport.getErrorBy(ErrorConstant.MEMBER_NOT_EXISTS_ERROR);
        }

        try {

            Long companyId = passenger.getCompanyId();

            Company appointCompany = null;
            if (StringUtils.isNotBlank(acKey)) {
                appointCompany = companyService.findByAppKey(acKey);
            }

            if (null != appointCompany) {
                companyId = appointCompany.getId();
            }

            Page<Map<String, Object>> activitys = activityService
                .queryByCompany(companyId, page, limit);

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstant.SUCCESS);
            instance.setData(activitys);

            return instance;

        } catch (Exception e) {
            logger.error("api v3 queryActivitys error: {}", e);
            return ResultTransport.getErrorBy(ErrorConstant.SERVER_ERROR);
        }

    }


}
